#coding=utf-8
import time
import subprocess

class MonkeyRunner:
    def __init__(self):
        pass

    def press(self):
        pass

    def shell_multiple(self):

        p= subprocess.Popen("monkeyrunner", shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        time.sleep(5)
        # print("---")
        # p.stdin.write("from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage")
        # p.stdin.flush()
        # p.stdin.write("device = MonkeyRunner.waitForConnection()\n")
        #
        # p.stdin.flush()
        # print("=========")
        # p.stdin.write("device.startActivity(component='com.tcl.usercenter/.WelcomActivity')\n")
        # p.stdin.flush()
        # p.stdin.write("device.press('KEYCODE_BACK','DOWN_AND_UP')\n")
        # p.stdin.flush()
        #
        # p.stdin.write("device.startActivity(component='com.tcl.cyberui/.MainActivity')\n")
        # p.stdin.flush()

        p.stdin.writelines(
            "from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage\ndevice = MonkeyRunner.waitForConnection()\n")
        p.stdin.flush()
        p.stdin.write("device.startActivity(component='com.tcl.usercenter/.WelcomActivity')\n")
        p.stdin.flush()

        # print p.communicate("exit\n")
        pass



if __name__ == '__main__':
    mr=MonkeyRunner()
    # mr.shell_multiple()
    p = subprocess.Popen("monkeyrunner", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    time.sleep(10)
    # # p.communicate()
    # p.stdin.write("cd /data/local/tmp\n")
    # p.stdin.flush()
    # # print p.communicate(" cd /data/local/tmp\nexit\n")
    # # print("12345")
    # print p.communicate("ls\nexit\n")[0]

    abc='''
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage\n
device = MonkeyRunner.waitForConnection()\n
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')\n
device.press('KEYCODE_BACK','DOWN_AND_UP')\n
device.startActivity(component='com.tcl.usercenter/.WelcomActivity')
    '''
    p.stdin.write(abc)
    p.stdin.flush()
    pass